﻿namespace EFS.RC.Presences.Controls.Lync.Presences
{
	/// <summary>
	/// Interaction logic for List.xaml
	/// </summary>
	public partial class ListHorizontal : EFS.RC.Presences.Controls.Lync.Presences.List
	{
		// all colss
		private System.Collections.Generic.Dictionary<System.String, System.Windows.Controls.ColumnDefinition> gridColumnDefinitions = new System.Collections.Generic.Dictionary<System.String, System.Windows.Controls.ColumnDefinition>();
		private System.Collections.Generic.Dictionary<System.String, EFS.RC.Presences.Controls.Lync.Presences.Presence> gridPresences = new System.Collections.Generic.Dictionary<System.String, EFS.RC.Presences.Controls.Lync.Presences.Presence>();

		public ListHorizontal()
		{
			InitializeComponent();
		}

		/// <summary>
		/// Load presences
		/// </summary>
		/// <param name="sipArray"></param>
		public override void LoadPresences(System.String[] sipArray)
		{
			// new sips
			System.Collections.Generic.List<System.String> sipList = new System.Collections.Generic.List<System.String>(sipArray);
			System.Collections.Generic.List<System.String> newSipList = new System.Collections.Generic.List<System.String>();
			System.Collections.Generic.List<System.String> removeSipList = new System.Collections.Generic.List<System.String>();

			// add new 
			foreach (System.String sip in sipArray)
			{
				// check
				if (!gridColumnDefinitions.ContainsKey(sip))
				{
					// add
					newSipList.Add(sip);
				}
			}

			// add remove
			foreach (System.String sip in gridColumnDefinitions.Keys)
			{
				// check
				if (!sipList.Contains(sip))
				{
					// add
					removeSipList.Add(sip);
				}
			}

			// remove
			foreach (System.String sip in removeSipList)
			{
				// remove
				gridControlPresences.ColumnDefinitions.Remove(gridColumnDefinitions[sip]);
				gridControlPresences.Children.Remove(gridPresences[sip]);
			}

			// add
			foreach (System.String sip in newSipList)
			{
				// add head column definition
				System.Windows.Controls.ColumnDefinition colDef = new System.Windows.Controls.ColumnDefinition();
				colDef.Width = new System.Windows.GridLength(1, System.Windows.GridUnitType.Star);
				gridControlPresences.ColumnDefinitions.Add(colDef);
				gridColumnDefinitions[sip] = colDef;

				// presence
				EFS.RC.Presences.Controls.Lync.Presences.Presence controlPresence = new EFS.RC.Presences.Controls.Lync.Presences.Presence();
				controlPresence.SIP = sip;
				controlPresence.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
				controlPresence.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
				controlPresence.Margin = new System.Windows.Thickness(15, 0, 0, 0);
				gridControlPresences.Children.Add(controlPresence);
				gridPresences[sip] = controlPresence;

				// set position
				System.Windows.Controls.Grid.SetColumn(controlPresence, gridControlPresences.ColumnDefinitions.Count - 1);
				System.Windows.Controls.Grid.SetRow(controlPresence, 0);
			}
		}
	}

}